package com.yunji.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunji.news.entity.EnrollmentApplication;
import com.yunji.news.mapper.EnrollmentApplicationMapper;
import com.yunji.news.service.EnrollmentApplicationService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;

/**
 * 报名申请服务实现类
 *
 * @author yunji
 * @since 2024-11-26
 */
@Service
public class EnrollmentApplicationServiceImpl extends ServiceImpl<EnrollmentApplicationMapper, EnrollmentApplication> 
        implements EnrollmentApplicationService {

    @Override
    public IPage<EnrollmentApplication> getApplicationPage(Integer page, Integer size, 
                                                          Long schoolId, Long majorId, 
                                                          String status, String keyword) {
        Page<EnrollmentApplication> pageParam = new Page<>(page, size);
        return baseMapper.selectApplicationPage(pageParam, schoolId, majorId, status, keyword);
    }

    @Override
    public boolean submitApplication(EnrollmentApplication application) {
        application.setApplicationStatus("submitted");
        application.setApplicationTime(LocalDateTime.now());
        application.setPaymentStatus("pending");
        application.setStatus(1);
        return save(application);
    }

    @Override
    public boolean updateApplicationStatus(Long id, String status, String remark) {
        LambdaUpdateWrapper<EnrollmentApplication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnrollmentApplication::getId, id)
               .set(EnrollmentApplication::getApplicationStatus, status)
               .set(EnrollmentApplication::getRemark, remark);
        return update(wrapper);
    }

    @Override
    public boolean updatePaymentStatus(Long id, String paymentStatus) {
        LambdaUpdateWrapper<EnrollmentApplication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnrollmentApplication::getId, id)
               .set(EnrollmentApplication::getPaymentStatus, paymentStatus);
        
        if ("paid".equals(paymentStatus)) {
            wrapper.set(EnrollmentApplication::getPaymentTime, LocalDateTime.now());
        }
        
        return update(wrapper);
    }

    @Override
    public boolean updateExamScore(Long id, Integer examScore) {
        LambdaUpdateWrapper<EnrollmentApplication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnrollmentApplication::getId, id)
               .set(EnrollmentApplication::getExamScore, examScore)
               .set(EnrollmentApplication::getExamStatus, "completed");
        return update(wrapper);
    }

    @Override
    public boolean batchUpdateFinalStatus(Long[] ids, String finalStatus) {
        LambdaUpdateWrapper<EnrollmentApplication> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(EnrollmentApplication::getId, Arrays.asList(ids))
               .set(EnrollmentApplication::getFinalStatus, finalStatus);
        return update(wrapper);
    }
}